<template>
  <view class="content">
    <view class="mb-20 overflow" v-for="(item, index) in dataSource" :key="index">
      <tm-cell
        v-for="el in item"
        :key="el.path"
        :margin="[0, 0]"
        bottomBorder
        :border="2"
        :titleFontSize="30"
        :title="el.title"
        :rightText="el.rightTitle"
        :rightTextSize="28"
        @click="cellClick(el)"
      ></tm-cell>
    </view>
  </view>
</template>

<script setup lang="ts">
import { zconfirm } from "@/utils/util";
import { clearCache, getCashSzie } from "../utils/util";
import { reactive, ref } from "vue";
import { toast } from "../../tmui/tool/function/util";
interface cellType {
  title: string;
  type: string; //jump 跳路由 click是触发对应的点击事件
  path: string;
  rightTitle?: string;
}
const cashSize = ref("");
// 列表展示的数据
const dataSource = reactive([
  [
    {
      title: "员工签名",
      type: "jump",
      path: "/pages_mine/staff-sign/staff-sign",
      rightTitle: ""
    },
    {
      title: "修改密码",
      type: "jump",
      path: "/pages_mine/change-password/change-password",
      rightTitle: ""
    },
    {
      title: "清除缓存",
      type: "click",
      path: "clearCashMethods",
      rightTitle: cashSize
    },
    {
      title: "字体大小",
      type: "jump",
      path: "/pages_mine/set-fontsize/set-fontsize",
      rightTitle: ""
    }
  ],
  [
    // #ifdef APP
    {
      title: "检查版本",
      type: "click",
      path: "checkVersionMethods",
      rightTitle: ""
    },
    // #endif

    {
      title: "消息通知",
      type: "jump",
      path: "/pages_mine/message-notification/message-notification",
      rightTitle: ""
    }
  ],
  [
    {
      title: "分享给同事",
      type: "jump",
      path: "/pages_mine/share-colleague/share-colleague",
      rightTitle: ""
    },
    {
      title: "关于我们",
      type: "jump",
      path: "/pages_mine/about-us/about-us",
      rightTitle: ""
    },
    {
      title: "注销账号",
      type: "jump",
      path: "/pages_mine/close-account/close-account",
      rightTitle: ""
    }
  ]
]);

getmyCashSzie();

interface FunObject {
  checkVersionMethods: () => void;
  clearCashMethods: () => void;
  [key: string]: () => void;
}
const funObj: FunObject = {
  checkVersionMethods: () => {
    plus.runtime.getProperty(plus.runtime.appid as string, function (widgetInfo) {
      console.log("当前版本号：", widgetInfo.versionCode);
      // 获取当前的版本和服务端的版本比较然后再判断是否要升级
      const info = uni.getSystemInfoSync().platform;
      if (info == "ios") {
        //ios升级直接跳转到appstore
      } else if (info == "android") {
        // plus.downloader.createDownload();
        //安卓给个下载地址可以安装下载
        // plus.runtime.install("");
      }
    });
  },
  // 清除缓存的方法
  clearCashMethods: () => {
    if (cashSize.value == "0") {
      toast("暂无数据需要清理");
      return;
    }
    zconfirm("是否清理缓存", (isConfirm: boolean) => {
      if (isConfirm) {
        clearCache((size: string) => {
          cashSize.value = size;
        });
      }
    });
  }
};

//获取当前缓存数据大小
function getmyCashSzie() {
  getCashSzie((size: string) => {
    cashSize.value = size;
  });
}

function cellClick(item: cellType) {
  if (item.type == "jump") {
    uni.navigateTo({
      url: item.path
    });
  } else if (item.type == "click") {
    funObj[item.path]();
  }
}
</script>

<style lang="scss" scoped></style>
